package com.ytc.pquery.controller;

import com.ytc.position.model.Position;
import com.ytc.pquery.model.AddToLike;
import com.ytc.pquery.model.PositionDetails;
import com.ytc.pquery.model.Pquery;
import com.ytc.pquery.service.PqueryService;
import com.ytc.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

//职位查询
@Controller
@RequestMapping("pquery")
public class PqueryController {

    @Autowired
    private PqueryService pqueryService;

    //查询职位列表
    @RequestMapping("positionList")
    public String positionList(String dataIds, String positionName, Model model){
        List<PositionDetails> list = pqueryService.positionList(dataIds, positionName);
        model.addAttribute("list",list);
        System.out.println("==========");
        return "pquery/pqueryList";
    }

    //查询职位详情
    @RequestMapping("getPquery")
    public String getPqueryDetails(Integer positionId,Model model,HttpServletRequest request){
        User user =(User) request.getSession().getAttribute("user");
        PositionDetails pqueryDetails = pqueryService.getPqueryDetails(positionId);
        pqueryDetails.setUserId(user.getUserId());
        model.addAttribute("position",pqueryDetails);
        return "pquery/showOnePosition";
    }

    //收藏
    @RequestMapping("addToLike")
    @ResponseBody
    public String addToLike(Integer positionId, HttpServletRequest request){
        User user =(User) request.getSession().getAttribute("user");
        //addToLike.setUserId(user.getUserId());
        pqueryService.addToLike(positionId,user.getUserId());
        return "修改成功";
    }

    //获取收藏数据
    @RequestMapping("getAddToLike")
    @ResponseBody
    public AddToLike getAddToLike(Integer positionId, HttpServletRequest request){
        User user =(User) request.getSession().getAttribute("user");
        //addToLike.setUserId(user.getUserId());
        AddToLike addToLike = pqueryService.getAddToLike(positionId, user.getUserId());

        return addToLike;
    }


}
